package classic150;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <h3>leetcode</h3>
 *
 * @author BigFaceCat
 * @date 2023/11/24 17:05
 * @description <p>leetcode169-多数元素-数组-简单题</p>
 */
public class No169 {
    public static void main(String[] args) {
        int[] nums = {2, 2, 1, 1, 1, 2, 2};

        System.out.println(majorityElement(nums));
    }

    public static Integer majorityElement(int[] nums) {
        int lens = nums.length >> 1;
        Map<Integer, Integer> map = Arrays.stream(nums).boxed().collect(Collectors.toMap(
                k -> k,
                o -> 1,
                (o1, o2) -> o1 + 1));

        for (Integer key : map.keySet()) {
            if (map.get(key) > lens){
                return key;
            }
        }

        return null;
    }
}
